package com.wang.learn.cloudredis.controller;

import com.wang.learn.cloudredis.entity.Book;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * ClassName RedisDataController
 *
 * @author wang
 * Date 2022/3/9 10:13
 */
@RestController
@RequestMapping("/redis/data")
public class RedisDataController {

    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/list")
    public void list() {

        String[] user1 = new String[]{"1", "pan_junbiao的博客", "您好，欢迎访问 pan_junbiao的博客"};
        String[] user2 = new String[]{"2", "pan_junbiao的博客", "https://blog.csdn.net/pan_junbiao"};
        String[] user3 = new String[]{"3", "pan_junbiao的博客", "您好，欢迎访问 pan_junbiao的博客"};

//        redisTemplate.opsForList().rightPushAll("user1",user1);
//        redisTemplate.opsForList().rightPushAll("user2",user2);
//        redisTemplate.opsForList().rightPushAll("user3",user3);

        System.out.println(redisTemplate.opsForList().range("user1", 0, -1));
        System.out.println(redisTemplate.opsForList().range("user2", 0, -1));
        System.out.println(redisTemplate.opsForList().range("user3", 0, -1));

//        redisTemplate.opsForList().rightPush("userInfo",1);
//        redisTemplate.opsForList().rightPush("userInfo","pan_junbiao的博客");
//        redisTemplate.opsForList().rightPush("userInfo","https://blog.csdn.net/pan_junbiao");
//        redisTemplate.opsForList().rightPush("userInfo","您好，欢迎访问 pan_junbiao的博客");

//        redisTemplate.opsForList().rightPush("userInfo",2);
//        redisTemplate.opsForList().leftPush("userInfo",2);

        System.out.println("用户编号：" + redisTemplate.opsForList().index("userInfo", 0));
        System.out.println("用户名称：" + redisTemplate.opsForList().index("userInfo", 1));
        System.out.println("博客地址：" + redisTemplate.opsForList().index("userInfo", 2));
        System.out.println("博客信息：" + redisTemplate.opsForList().index("userInfo", 3));
    }


    @RequestMapping("/hash")
    public void hash() {

        Map<String, Object> userMap = new HashMap<>();
        userMap.put("userName", "pan_junbiao的博客");
        userMap.put("blogRemark", "您好，欢迎访问 pan_junbiao的博客");
        redisTemplate.opsForHash().putAll("userHash", userMap);
        System.out.println(redisTemplate.opsForHash().entries("userHash"));


        redisTemplate.opsForHash().put("userHash", "userName", "pan_junbiao的博客");
        redisTemplate.opsForHash().put("userHash", "blogUrl", "https://blog.csdn.net/pan_junbiao");
        redisTemplate.opsForHash().put("userHash", "blogRemark", "您好，欢迎访问 pan_junbiao的博客");
        System.out.println("用户名称：" + redisTemplate.opsForHash().get("userHash", "userName"));
        System.out.println("博客地址：" + redisTemplate.opsForHash().get("userHash", "blogUrl"));
        System.out.println("博客信息：" + redisTemplate.opsForHash().get("userHash", "blogRemark"));

        System.out.println("散列存储的值：" + redisTemplate.opsForHash().values("userHash"));
        System.out.println("散列存储的键：" + redisTemplate.opsForHash().keys("userHash"));

    }

    @RequestMapping("/set")
    public void set() {

        String[] citys = new String[]{"北京", "上海", "广州", "深圳"};
        System.out.println(redisTemplate.opsForSet().add("citySet", citys));
        System.out.println(redisTemplate.opsForSet().add("citySet", "香港", "澳门", "台湾"));
        //返回集合中的所有元素
        System.out.println(redisTemplate.opsForSet().members("citySet"));
        //随机弹出
        System.out.println(redisTemplate.opsForSet().pop("citySet"));
        System.out.println(redisTemplate.opsForSet().members("citySet"));

    }

    @RequestMapping("/sortSet")
    public void sortSet() {

        ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>("pan_junbiao的博客_01",1.1);
        ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>("pan_junbiao的博客_02",2.2);
        ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>("pan_junbiao的博客_03",3.3);

        Set<ZSetOperations.TypedTuple<String>> typles = new HashSet<>();
        typles.add(objectTypedTuple1);
        typles.add(objectTypedTuple2);
        typles.add(objectTypedTuple3);

        System.out.println(redisTemplate.opsForZSet().add("typles",typles));
        System.out.println(redisTemplate.opsForZSet().range("typles",0,-1));

        System.out.println(redisTemplate.opsForZSet().add("zset2", "pan_junbiao的博客_01", 9.6));
        System.out.println(redisTemplate.opsForZSet().add("zset2", "pan_junbiao的博客_01", 9.6));


    }


}
